﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Linq;
using System.Text;

namespace MyNetflix2
{
    public class RequestToken
    {
        #region locals

        private string _Token;
        private string _TokenSecret;
        private string _PermissionUrl;
        private string _ApplicationName;
        private Connection _Connection;
        private string _RawData;

        #endregion

        #region Public

        public string RawData
        {
            get { return _RawData; }
        }
        public string Token
        {
            get { return _Token; }
        }
        public string TokenSecret
        {
            get { return _TokenSecret; }
        }
        public string ApplicationName
        {
            get { return _ApplicationName; }
        }
        public string PermissionURL
        {
            get { return _PermissionUrl; }
        }

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="rawRequestToken"></param>
        /// <param name="conn"></param>
        public RequestToken(string rawRequestToken, Connection conn)
        {
            //--------------------------------------------------------------------------------
            // First Get the parsedData 
            //--------------------------------------------------------------------------------
            ClrExtensions.Net.Rest.QueryParameterCollection parsedData = new ClrExtensions.Net.Rest.QueryParameterCollection(rawRequestToken);

            //--------------------------------------------------------------------------------
            // Store the connection, token, token secret, etc...
            //--------------------------------------------------------------------------------
            _Connection = conn;
            _RawData = rawRequestToken;
            _Token = parsedData["oauth_token"];
            _TokenSecret = parsedData["oauth_token_secret"];
            _ApplicationName = parsedData["application_name"];
            _PermissionUrl = RestCalls.PermissionLogin(conn, this).ToUrlString();
        }

        /// <summary>
        /// Covert the token to an access token
        /// </summary>
        /// <returns></returns>
        public AccessToken ConvertToAccessToken()
        {
            //--------------------------------------------------------------------------------
            // Convert the request to an Access Token
            //--------------------------------------------------------------------------------
            return _Connection.ConvertRequestToAccessToken(this);
        }
    }
}
